{% extends '../layout.html' %}

{% block body %}
<section class="content-header">
    <h1>
        管理员管理
        <small></small>
    </h1>
</section>

<!-- Main content -->
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <!-- general form elements -->
            <div class="box">
                <div class="box-header with-border">
                    <h3 class="box-title">新增管理员</h3>
                </div>
                <!-- form start -->
                <form class="form-horizontal" id="add-form" method="post">
                    <div class="box-body">
                        <input class="form-control" name="id" placeholder="" value="{{administrator._id}}"
                               type="hidden">

                        <div class="form-group">
                            <label class="col-sm-2 control-label">用户名</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="username" placeholder="" value="{{administrator.username}}"
                                       type="text">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">手机号</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="mobile" placeholder="" value="{{administrator.mobile}}"
                                       type="number">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">密码</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="password" placeholder="请输入新密码" value=""
                                       type="text">
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">邮箱</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="email" placeholder="" value="{{administrator.email}}"
                                       type="email">
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="control-label col-md-2">状态</label>

                            <div class="col-md-7">
                                <label class="radio-inline"><input name="status" type="radio"
                                                                   {% if administrator.status == 1 %}checked="checked" {% endif %}
                                    value="1"><span>开启</span></label>
                                <label class="radio-inline"><input name="status" type="radio"
                                                                   {% if administrator.status == 0 %}checked="checked" {% endif %}
                                    value="0"><span>关闭</span></label>
                            </div>
                        </div>

                        <div class="form-group">
                            <label class="col-sm-2 control-label">备注</label>

                            <div class="col-sm-10">
                                <input class="form-control" name="remark" placeholder="" value="{{administrator.remark}}"
                                       type="text">
                            </div>
                        </div>

                        <div class="box-footer">
                            <div class="col-sm-2">
                                <button type="button" id="btnSave" class="btn btn-block btn-danger">保存</button>
                            </div>

                            <div class="col-sm-2">
                                <button type="button" class="btn btn-block btn-default" onclick="history.go(-1)">取消
                                </button>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
            <!-- /.box -->
        </div>
        <!--/.col (right) -->
    </div>
</section>

<script>
    $(function () {
        $("#btnSave").click(function () {
            doPost();
        });
    });

    function doPost(){
        const index = layer.load(0, {time: 5 * 1000}); //显示加载中动画，最多5秒自动关闭
        $.ajax({
            type: "post",
            url: `/admin/administrator/add`,
            data: $("#add-form").serializeArray(),
            beforeSend: function (xhr) {
                xhr.setRequestHeader('x-pjax', 'true');
            },
            success: function (res) {
                if (res.errno === 0){
                    location.href =res.data;
                }else {
                    layer.msg(res.errmsg, {icon: 2});
                }

                console.log(res);

                layer.close(index); //关闭加载动画
            },
            error: function (xhr) {
                layer.close(index); //关闭加载动画
                if (xhr.status === 401) {
                    layer.msg('未授权访问接口！', {icon: 2});
                } else {
                    layer.msg('通讯失败！请重试！', {icon: 2});
                }
            }
        });
    }
</script>

{% endblock %}